package me.send.produce.client.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.send.business.base.Page;
import me.send.business.service.bean.AdvertBean;
import me.send.business.service.bean.HomeBean;
import me.send.business.service.bean.OrderBean;
import me.send.business.service.bean.ReturnJsonBean;
import me.send.business.service.service.IndexService;
import me.send.framework.util.HelperUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="index")
public class IndexController {
	
	@Autowired
	private IndexService indexService;
	
//	@RequestMapping("home")
//	@ResponseBody
//	public Map<String, Object> home(HttpServletRequest request,String devicenumber,Page page,Integer userid) {
//		return indexService.home(request,devicenumber,page,userid);
//	}
	
	@RequestMapping("home2")
	@ResponseBody
	public Map<String, Object> home2(HttpServletRequest request,String devicenumber,Page page,int userid){
		return indexService.home2(request,devicenumber,page,userid);
	}
	
	/**
	 * 测试注册接口
	 */
	@RequestMapping(value="code")
	public String code(final String phonenumber,HttpServletRequest request){
		System.out.println(phonenumber);
		// 每个人进来 都存一下手机号码 
		indexService.code(phonenumber);
		return "/validate.jsp";
	}
	
	@RequestMapping(value="validate")
	@ResponseBody
	public Map<String, Object> validate(String phonenumber,String code) {
		return indexService.validate(phonenumber, code);
	}
	
	@RequestMapping(value = "index")
	@ResponseBody
	public HomeBean index(String devicenumber, int startid,int size,Integer userid,String cityname){
		return indexService.index(devicenumber, startid, size, userid, cityname);
	}
	
	@RequestMapping(value = "orders/index")
	@ResponseBody
	public ReturnJsonBean ordersIndex(String devicenumber, int startid,int size,Integer userid,String cityname){
		ReturnJsonBean rb = new ReturnJsonBean();
		List<OrderBean> list = indexService.ordersIndex(devicenumber, startid, size, userid, cityname);
		if (list!=null) {
			rb.setCode(ReturnJsonBean.SUCCESS);
			rb.setMessage(ReturnJsonBean.SUCCESS_STR);
			rb.setDatas(list);
		}
		return rb;
	}
	
	@RequestMapping(value="advert/list")
	@ResponseBody
	public ReturnJsonBean advertList(String devicenumber,String cityname) {
		ReturnJsonBean rb = new ReturnJsonBean();
		try {
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("cityname", cityname);
			p.put("currentDate", HelperUtil.getCurrentDate());
			AdvertBean advertBean = indexService.advertList(cityname);
			if (advertBean!=null) {
				rb.setDatas(advertBean);
				rb.setCode(ReturnJsonBean.SUCCESS);
				rb.setMessage(ReturnJsonBean.SUCCESS_STR);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rb;
	}
	
	@RequestMapping(value = "orders/tasking")
	@ResponseBody
	public ReturnJsonBean ordersTasking(String devicenumber, int startid,int size,Integer userid,String cityname){
		ReturnJsonBean rb = new ReturnJsonBean();
		List<OrderBean> list = indexService.ordersTasking(devicenumber, startid, size, userid, cityname);
		if (list!=null) {
			rb.setCode(ReturnJsonBean.SUCCESS);
			rb.setMessage(ReturnJsonBean.SUCCESS_STR);
			rb.setDatas(list);
		}
		return rb;
	}
	
	@RequestMapping(value = "tasking")
	@ResponseBody
	public HomeBean tasking(String devicenumber, int startid,int size,Integer userid,String cityname){
		return indexService.tasking(devicenumber, startid, size, userid, cityname); 
	}
}
